﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Npgsql;
using NpgsqlTypes;


namespace Machop
{
    public class DatabaseConnection
    {
        

        private NpgsqlConnection connection;
        private DataSet ds;
        private DataTable dt;
        private NpgsqlDataAdapter dataAdapter;



        public DatabaseConnection(string server, string port, string user, string password, string databasename) 
        {
            try
            {

                string connstring = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};", 
                    server, port, user, password, databasename);

                connection = new NpgsqlConnection(connstring);

        
               

                
            }
            catch (Exception msg)
            {
                // something went wrong, and you wanna know why
                //MessageBox.Show(msg.ToString());
 
                //throw;
            }
        }

        public DataTable SendQuery(string query) 
        {
            ds = new DataSet();
            dt = new DataTable();
            dataAdapter = new NpgsqlDataAdapter(query, connection);
            ds.Reset();
            dataAdapter.Fill(ds);
            if(ds.Tables.Count >0 )
                dt = ds.Tables[0];
            
            return dt;
        }

        public bool disconnect()
        {
            connection.Close();

            return true;
        }
    }
}
